package com.example.demo4.webapi;


<<<<<<< HEAD
import com.example.demo4.core.PageUtils;
=======
>>>>>>> bcb091a17987267f0d65e4154f997543fe5bec34
import com.example.demo4.domain.Student;

import com.example.demo4.service.StudentService;

import org.springframework.beans.factory.annotation.Autowired;
<<<<<<< HEAD
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
=======
>>>>>>> bcb091a17987267f0d65e4154f997543fe5bec34
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/webapi/student")
public class StudentRestController {
    @Autowired
    private StudentService studentService;


    @GetMapping("/list")
    public List<Student> getAll() {
        List<Student> students=studentService.findAll();
        return students;
    }
<<<<<<< HEAD


=======
>>>>>>> bcb091a17987267f0d65e4154f997543fe5bec34
    @GetMapping("/{name}")
    public List<Student> findByNameLike(@PathVariable String name){

        System.out.println(name);
        List<Student> students =studentService.findByNameLike("%"+name+"%");
        return students;
    }
    @GetMapping("/get/{id}")
    public Student getById(@PathVariable Integer id){

        Student student = studentService.findById(id);
        System.out.println(student);
        return student;
    }



    @PostMapping("/insert")
    public Student save( Student student){

        Student insert = studentService.insert(student);
        return insert;
    }

    @PutMapping("/update")
    public void update(Student student){
        studentService.update(student);

    }

    @DeleteMapping("/delete/{id}")
    public void deleteBySno(@PathVariable Integer id){
        System.out.println("step1");
        studentService.deleteById(id);
    }
<<<<<<< HEAD
    @GetMapping("/getbypage")
    public PageUtils getByPage(@RequestParam(value = "page",defaultValue = "0" )Integer page,
                               @RequestParam(value = "size",defaultValue = "10") Integer size) {
        // Sort构造方法已经私有
        // id倒序
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        Pageable pageable =PageRequest.of(page,size,sort);
        Page<Student> studentPage =studentService.findAll(pageable);
//        return studentPage
        PageUtils pageUtils = new PageUtils(studentPage.getContent(),studentPage.getTotalElements());
        return  pageUtils;
    }

=======
>>>>>>> bcb091a17987267f0d65e4154f997543fe5bec34
}
